<?php
// fix_issues.php - 修复所有问题
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>修复项目问题</h1>";

// 创建缺失的页面文件
$pages = [
    'about.php' => 'About Page Content',
    'contact.php' => 'Contact Page Content'
];

foreach ($pages as $page => $content) {
    if (!file_exists($page)) {
        $simpleContent = "<?php
// $page - Simple Version
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset=\"UTF-8\">
    <title>" . ucfirst(str_replace('.php', '', $page)) . " - Global Food Market</title>
    <link rel=\"stylesheet\" href=\"css/style.css\">
</head>
<body>
    <header style=\"background: #2c5530; color: white; padding: 1rem 0;\">
        <div class=\"container\">
            <h1>Global Food Market</h1>
            <nav>
                <a href=\"index.php\">Home</a> | 
                <a href=\"products.php\">Products</a> | 
                <a href=\"about.php\">About</a> | 
                <a href=\"contact.php\">Contact</a>
            </nav>
        </div>
    </header>
    
    <div class=\"container\" style=\"padding: 2rem;\">
        <h1>" . ucfirst(str_replace('.php', '', $page)) . " Page</h1>
        <p>This is a temporary " . str_replace('.php', '', $page) . " page.</p>
        <p><a href=\"index.php\">Return to Home</a></p>
    </div>
    
    <footer style=\"background: #333; color: white; padding: 2rem 0; margin-top: 3rem;\">
        <div class=\"container\">
            <p style=\"text-align: center;\">&copy; 2024 Global Food Market</p>
        </div>
    </footer>
</body>
</html>";
        
        if (file_put_contents($page, $simpleContent)) {
            echo "<p style='color: green;'>✓ 创建页面: $page</p>";
        } else {
            echo "<p style='color: red;'>✗ 无法创建页面: $page</p>";
        }
    } else {
        echo "<p style='color: orange;'>✓ 页面已存在: $page</p>";
    }
}

// 修复JSON中的图片链接
$jsonFile = 'data/products.json';
if (file_exists($jsonFile)) {
    $products = json_decode(file_get_contents($jsonFile), true);
    
    $updated = false;
    foreach ($products as &$product) {
        // 确保图片URL有正确的参数
        if (strpos($product['image_url'], 'unsplash.com') !== false) {
            if (strpos($product['image_url'], '?') === false) {
                $product['image_url'] .= '?w=400&auto=format&fit=crop&q=60';
                $updated = true;
            }
        }
    }
    
    if ($updated) {
        file_put_contents($jsonFile, json_encode($products, JSON_PRETTY_PRINT));
        echo "<p style='color: green;'>✓ 修复JSON图片链接</p>";
    }
}

echo "<h2>修复完成！</h2>";
echo "<p><a href='debug.php'>检查状态</a> | <a href='index.php'>访问首页</a></p>";
?>